package com.jltx.xq.simplewatch.db.helper;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.jltx.xq.simplewatch.base.App;
import com.jltx.xq.simplewatch.db.SimpleWatchContract;
import com.jltx.xq.simplewatch.news.model.bean.NewsState;
import com.jltx.xq.simplewatch.utils.IOUtils;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/26.
 */

public class NewsStateDBHelper {
    private static Uri newsStateUri = SimpleWatchContract.CONTENT_NEWS_STATE_URI;

    /**
     * 添加新的新闻状态
     * @param channel
     * @param key
     * @param isRead
     * @param isDel
     */
    public static void addNewsState(String channel,String key,int isRead,int isDel){
        ContentValues cv = new ContentValues();
        cv.put(SimpleWatchContract.NewsStateColumns.CHANNEL,channel);
        cv.put(SimpleWatchContract.NewsStateColumns.KEY,key);
        cv.put(SimpleWatchContract.NewsStateColumns.IS_READ,isRead);
        cv.put(SimpleWatchContract.NewsStateColumns.IS_DELETE,isDel);
        App.getApp().getContentResolver().insert(newsStateUri,cv);
    }

    /**
     * 更新新闻的状态
     * @param channel
     * @param key
     * @param isRead
     * @param isDel
     */
    public static void updateNewsState(String channel,String key, int isRead,int isDel){
        ContentValues cv = new ContentValues();
        cv.put(SimpleWatchContract.NewsStateColumns.IS_READ,isRead);
        cv.put(SimpleWatchContract.NewsStateColumns.IS_DELETE,isDel);
        //更新条件
        String selection = SimpleWatchContract.NewsStateColumns.CHANNEL + " =? AND " + SimpleWatchContract.NewsStateColumns.KEY + " =? ";
        String[] selectionArgs = new String[]{channel,key};
        App.getApp().getContentResolver().update(newsStateUri,cv,selection,selectionArgs);
    }

    /**
     * 判断channel和key对应的新闻是否已读这些状态记录到数据库
     * @param channel
     * @param key
     * @return
     */
    public static boolean isExitNewsState(String channel,String key){
        //查询和过滤条件
        String selection = SimpleWatchContract.NewsStateColumns.CHANNEL + " =? AND " + SimpleWatchContract.NewsStateColumns.KEY + " =? ";
        String[] selectionArgs = new String[]{channel,key};
        Cursor cursor = null;
        boolean result = false;
        try {
            cursor = App.getApp().getContentResolver().query(newsStateUri,null,selection,selectionArgs,null);
            if(cursor != null && cursor.moveToNext()){
                result = true;
            }
        }finally {
            IOUtils.closeIO(cursor);
        }
        return result;
    }

    /**
     *
     * @param channel
     * @param key
     * @param isRead
     * @param isDel
     */
    public static void addOrUpdateNewsState(String channel,String key,int isRead,int isDel){
        //判断是否已经存在
        if(isExitNewsState(channel,key)){
            //存在则更新状态
            updateNewsState(channel,key,isRead,isDel);
        }else {
            //不存在则添加
            addNewsState(channel,key,isRead,isDel);
        }
    }


    /**
     * 通过channel和key去查询出对应的新闻删除状态和已读两个两个状态
     * @param channel
     * @param key
     * @return
     */
    public static NewsState getNewsState(String channel,String key){
        String[] proj = new String[]{
                SimpleWatchContract.NewsStateColumns.CHANNEL
                ,SimpleWatchContract.NewsStateColumns.KEY
                ,SimpleWatchContract.NewsStateColumns.IS_READ
                ,SimpleWatchContract.NewsStateColumns.IS_DELETE
        };
        //查询和过滤条件
        String selection = SimpleWatchContract.NewsStateColumns.CHANNEL + " =? AND " + SimpleWatchContract.NewsStateColumns.KEY + " =? ";
        String[] selectionArgs = new String[]{channel,key};
        Cursor cursor = null;
        NewsState result = null;
        try {
            cursor = App.getApp().getContentResolver().query(newsStateUri,proj,selection,selectionArgs,null);
            if(cursor != null && cursor.moveToNext()){
                result = new NewsState();
                result.setChannel(cursor.getString(cursor.getColumnIndex(SimpleWatchContract.NewsStateColumns.CHANNEL)));
                result.setKey(cursor.getString(cursor.getColumnIndex(SimpleWatchContract.NewsStateColumns.KEY)));
                result.setIs_read(cursor.getInt(cursor.getColumnIndex(SimpleWatchContract.NewsStateColumns.IS_READ)));
                result.setIs_del(cursor.getInt(cursor.getColumnIndex(SimpleWatchContract.NewsStateColumns.IS_DELETE)));
            }
        }finally {
            IOUtils.closeIO(cursor);
        }
        return result;
    }

    /**
     *
     * @param channel
     * @param key
     * @return
     */
    public static NewsState getNewsStateNoNull(String channel,String key){
        NewsState newsState = getNewsState(channel,key);
        return newsState != null ? newsState : new NewsState();
    }

}
